﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Net;
using System.Web;

namespace FaceSharp.Api.Common
{
    public static class ImageUtil
    {
        public static Image CropImage(Image imgToCrop, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(imgToCrop);
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        public static Image CropAndFill(Image imgToResize, Size size)
        {
            float nPercent = 0;
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            var startWidth = 0;
            var startHeight = 0;

            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
            {
                nPercent = nPercentW;
            }
            else
            {
                nPercent = nPercentH;
            }
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            startWidth = (size.Width - destWidth) < 0 ? (size.Width - destWidth) / 2 : 0;
            startHeight = (size.Height - destHeight) < 0 ? (size.Height - destHeight) / 2 : 0;

            g.DrawImage(imgToResize, startWidth, startHeight, destWidth, destHeight);
            g.Dispose();
            return (Image)b;
        }

        public static Image ResizeImage(Image imgToResize, Size size, bool cropAndFill)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (!cropAndFill) // fill smallest dimension - no crop
            {
                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;
            }
            else // fill largest dimension - crop
            {
                if (nPercentH > nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            var startWidth = 0;
            var startHeight = 0;
            if (!cropAndFill)
            {
                startWidth = (size.Width - destWidth) > 0 ? (size.Width - destWidth) / 2 : 0;
                startHeight = (size.Height - destHeight) > 0 ? (size.Height - destHeight) / 2 : 0;
            }

            g.DrawImage(imgToResize, startWidth, startHeight, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        public static Image LoadImage(string url)
        {
            var backgroundUrl = url;
            WebRequest request = WebRequest.Create(backgroundUrl);
            WebResponse response = request.GetResponse();
            System.IO.Stream stream = response.GetResponseStream();
            return Image.FromStream(stream);
        }
    }
}